﻿using System;

namespace NFox.Pycad.Core.Modules
{

    public class panel
    {

        protected dynamic _func;

        public string Name { get; protected set; }

        public bool Lock { get; protected set; }

        public int Flags { get; }

        public Guid Guid { get; private set; }

        public string Text { get; set; }

        private dynamic _cls;

        private dynamic __docking__, __docked__;

        private dynamic _obj, _ps;

        public panel(string guid, string name = null, dynamic flags = null)
        {
            Name = name;
            Lock = false;
            if (flags == null)
                Flags = 0;
            else if (flags is int)
                Flags = flags;
            else
                Flags = flags.value__;
            Guid = new Guid(guid);
        }

        public dynamic __call__(dynamic args = null)
        {
            if(args == null)
            {
                var ed = Application.GetValue("ed");
                ed();
                var stdio = Application.GetValue("stdio");
                var console = stdio.console;
                try
                {
                    stdio.redirect(null);
                    if (_obj == null)
                    {
                        dynamic PaletteSet = Application.Engine.Execute("acws.PaletteSet");
                        _ps = PaletteSet(Text, Name, Guid);
                        _obj = _cls(_ps);
                    }
                    _ps.Visible = true;
                    if (__docking__ != null)
                    {
                        try { __docking__(_obj, _ps); }
                        catch { }
                    }
                    _ps.Dock = _obj.dockside;
                    if (__docked__ != null)
                    {
                        try { __docked__(_obj, _ps); }
                        catch { }
                    }
                }
                finally
                {
                    stdio.redirect(console);
                }
                return null;
            }
            else
            {
                _cls = args;
                if (Name == null) Name = _cls.__name__;

                var hasattr = Application.GetValue("hasattr");
                if (!hasattr(_cls, "dockside"))
                {
                    dynamic DockSides = Application.Engine.Execute("acws.DockSides");
                    Application.GetValue("setattr")(_cls, "dockside", DockSides.None);
                }

                var getattr = Application.GetValue("getattr");
                Text = 
                    hasattr(_cls, "text") ? 
                    getattr(_cls, "text") : 
                    _cls.__name__;
                if (hasattr(_cls, "__docking__"))
                    __docking__ = getattr(_cls, "__docking__");
                if (hasattr(_cls, "__docked__"))
                    __docked__ = getattr(_cls, "__docked__");

                pye.addcommand(this);
                return this;

            }


        }

    }

}
